package com.artron.mediaartron.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;

import com.artron.baselib.base.BaseFragment;
import com.artron.baselib.utils.UIUtils;
import com.artron.mediaartron.R;
import com.artron.mediaartron.ui.callback.MainActivityCallback;
import com.artron.mediaartron.ui.callback.ShowMainMenuItemCallback;
import com.artron.mediaartron.ui.callback.TitleChangeCallback;
import com.artron.mediaartron.ui.callback.ToolbarShowOrGoneCallback;
import com.artron.mediaartron.ui.fragment.factory.HomeFragmentFactory;
import com.artron.mediaartron.ui.fragment.home.MainFragment;
import com.artron.mediaartron.ui.fragment.login.LoginWrapperFragment;
import com.artron.mediaartron.ui.fragment.login.NoLoginFragment;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import static okhttp3.internal.Internal.instance;


public class MainActivity extends AppBaseActivity implements ToolbarShowOrGoneCallback,
        TitleChangeCallback,ShowMainMenuItemCallback {

    private boolean mMenuDeleteIsVisible;
    private boolean mMenuSettingIsVisible;

    public static void start(Context context) {
        Intent starter = new Intent(context, MainActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_general;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (fragments == null || fragments.size() == 0) {
            addFragment(R.id.General_content, MainFragment.newInstance(), false);
        } else {
            replaceFragment(R.id.General_content, MainFragment.newInstance(), false);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_page, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //菜单点击
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.findItem(R.id.menu_delete).setVisible(mMenuDeleteIsVisible);
        menu.findItem(R.id.menu_setting).setVisible(mMenuSettingIsVisible);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void setToolbarShow(boolean visible) {
        int visibleInt = visible ? View.VISIBLE : View.GONE;

        mToolbar.setVisibility(visibleInt);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            int color = visible ? R.color.white : R.color.transparent;
            getWindow().setStatusBarColor(UIUtils.getColor(color));
        }

        if (visible) {
            final WindowManager.LayoutParams attrs = getWindow().getAttributes();
            attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setAttributes(attrs);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } else {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }

        HomeFragmentFactory instance = HomeFragmentFactory.getInstance();
        switch (requestCode) {
            case NoLoginFragment.REQUEST_CODE:
                BaseFragment fragment1 = instance.getFragment(HomeFragmentFactory.SHOPPING_CART);
                ((LoginWrapperFragment) fragment1).resetView();
                BaseFragment centerFragment1 = instance.getFragment(HomeFragmentFactory.MY_CENTER);
                centerFragment1.onActivityResult(requestCode, resultCode, data);
                break;
            case GeneralActivity.REQUEST_CODE:
                BaseFragment centerFragment2 = instance.getFragment(HomeFragmentFactory.MY_CENTER);
                centerFragment2.onActivityResult(requestCode, resultCode, data);
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void setTitle(String title) {
        mTvTitle.setText(title);
    }

    public void showMainMenuItem(boolean isDeleteItem, boolean isSettingItem) {
        mMenuDeleteIsVisible = isDeleteItem;
        mMenuSettingIsVisible = isSettingItem;
        invalidateOptionsMenu();
    }

    @Override
    protected void onRestart() {
        super.onRestart();

        if (mCurrentFragment != null) {
            if (mCurrentFragment instanceof MainActivityCallback) {
                ((MainActivityCallback) mCurrentFragment).refreshData();
                ((MainActivityCallback) mCurrentFragment).initShoppingItemNum();
            }
        }
    }
}
